/*
 * Copyright (c) 2015. For Intelligent Group.
 */
package com.intelligent.ispc.client.core.view.analysis.grr.render;

import com.intelligent.ispc.client.core.modal.grr.FixViewDataModel;
import com.intelligent.ispc.client.utils.Colur;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.List;

/**
 * Created by cherry on 16/9/5.
 */
public class ViewDataTableCellRenderer extends JLabel implements TableCellRenderer {

    private FixViewDataModel tableModel;


    public ViewDataTableCellRenderer(JTable table) {
        tableModel = (FixViewDataModel) table.getModel();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        List<Integer> firstChanges = tableModel.getFirstPosition();
        JCheckBox checkbox = null;

        if (column == 0) {
            for (int i = 0; i < firstChanges.size(); i++) {
                if (row == firstChanges.get(i)) {
                    checkbox = setColumn1Component(value);
                }
            }
            if (checkbox != null) {
                return checkbox;

            } else {
                this.setText("");
                return this;
            }

        } else if (column == 1) {
            return setColumn1Component(value);
        } else {
            String text = "-";
            if (value != null) {
                text = value.toString();
            }
            this.setText(text);
            this.setHorizontalAlignment((int) CENTER_ALIGNMENT);
            return this;
        }
    }

    private JCheckBox setColumn1Component(Object value) {
        JCheckBox checkBox = new JCheckBox();
        checkBox.setBackground(Colur.WHITE);
        checkBox.setHorizontalAlignment((int) CENTER_ALIGNMENT);
        checkBox.setSelected(Boolean.valueOf(value.toString()));
        return checkBox;
    }
}
